package com.view.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.mvn_spring_jsf_jar.common.dao.BaseModelDAO;
import org.mvn_spring_jsf_jar.model.Cliente;
import org.mvn_spring_jsf_jar.query.ClienteQuery;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@FacesConverter("com.view.converter.ClienteConverter")
public class ClienteConverter implements Converter {

	@SuppressWarnings("unchecked")
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		Cliente cliente = null;
		if (arg2 != null && !"".equalsIgnoreCase(arg2)) {
			ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
					"applicationContext.xml");
			BaseModelDAO<Cliente, ClienteQuery> dao = (BaseModelDAO<Cliente, ClienteQuery>) ctx
					.getBean("clienteDAO");
			cliente = dao.get(arg2);
		}
		return cliente;
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		String ret = null;
		if (arg2 != null) {
			Cliente cliente = (Cliente) arg2;
			ret = cliente.getNombre();
		}
		return ret;
	}
}
